1
จากคำสั่งถึงสายงาน: ภาพรวมการจัดการลำดับงานใน LangChain
AI010Lesson 6
00:00

จากคำสั่งถึงสายงาน

วิวัฒนาการของการโต้ตอบกับโมเดลภาษา

ในบทเรียนก่อนหน้า เราเน้นไปที่การโต้ตอบแบบใช้คำสั่งเพียงครั้งเดียว อย่างไรก็ตาม แอปพลิเคชันในโลกความเป็นจริงต้องการมากกว่าแค่คำถามและคำตอบเพียงครั้งเดียว เพื่อสร้างระบบปัญญาประดิษฐ์ที่ขยายขนาดได้ เราจำเป็นต้องเปลี่ยนมาใช้การจัดการลำดับงาน. ซึ่งเกี่ยวข้องกับการเชื่อมต่อการเรียกใช้โมเดลภาษาหลายครั้งเข้าด้วยกัน การตัดสินใจแยกทางตามตรรกะที่อิงจากข้อมูลผู้ใช้ และอนุญาตให้โมเดลสามารถติดต่อกับข้อมูลภายนอกได้

องค์ประกอบพื้นฐานของการจัดการลำดับงาน

  • LLMChain: หน่วยพื้นฐาน มันรวมเทมเพลตคำสั่งกับโมเดลภาษาเข้าด้วยกัน
  • สายงานลำดับ: ช่วยให้คุณสร้างกระบวนการหลายขั้นตอน ซึ่งผลลัพธ์ของขั้นตอนหนึ่งจะกลายเป็นข้อมูลนำเข้าสำหรับขั้นตอนต่อไป
  • สายงานตัวควบคุมเส้นทาง: ทำหน้าที่เหมือน "ตัวควบคุมการจราจร" โดยใช้โมเดลภาษาในการตัดสินใจว่าสายงานย่อยเฉพาะเจาะจงใดควรจัดการคำขอเฉพาะเจาะจงหนึ่ง (ตัวอย่างเช่น ส่งคำถามคณิตศาสตร์ไปยัง "สายงานคณิตศาสตร์" และคำถามประวัติศาสตร์ไปยัง "สายงานประวัติศาสตร์")

หลักการสำคัญ: กฎของสายงาน

สายงานช่วยรวมองค์ประกอบหลายอย่าง เช่น โมเดล คำสั่ง และหน่วยความจำ เข้าไว้ด้วยกันเป็นแอปพลิเคชันเดียวที่สมบูรณ์ ความเป็นโมดูลาร์นี้ช่วยให้มั่นใจได้ว่างานซับซ้อนสามารถแบ่งย่อยออกเป็นขั้นตอนที่จัดการได้ง่ายและตรวจสอบข้อผิดพลาดได้

เคล็ดลับเฉพาะ: การตรวจสอบข้อผิดพลาดในสายงาน
เมื่อสายงานของคุณซับซ้อนมากขึ้น ให้ใช้langchain.debug = True. ฟีเจอร์นี้ช่วยให้คุณเห็นคำสั่งที่ส่งไปอย่างแม่นยำ และผลลัพธ์ดิบจากแต่ละขั้นตอนของสายงานได้อย่างชัดเจน
sequential_chain.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
Question 1
In LangChain, what is the primary difference between a SimpleSequentialChain and a standard SequentialChain?
SimpleSequentialChain supports multiple input variables, while SequentialChain does not.
SimpleSequentialChain only supports a single input and single output flowing between steps.
Only SequentialChain can be used with ChatOpenAI models.
Challenge: Library Support Router
Design a routing mechanism for a specialized bot.
You are building a support bot for a library.

Define the logic for a RouterChain that distinguishes between "Book Recommendations" and "Operating Hours."
Step 1
Create two prompt templates: one for book suggestions and one for library schedule info.
Solution:
book_template = """You are a librarian. Recommend books based on: {input}"""
schedule_template = """You are a receptionist. Answer hours queries: {input}"""

prompt_infos = [
    {"name": "books", "description": "Good for recommending books", "prompt_template": book_template},
    {"name": "schedule", "description": "Good for answering operating hours", "prompt_template": schedule_template}
]
Step 2
Define the router_template to guide the LLM on how to classify the user's intent, and initialize the chain.
Solution:
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(
    destinations=destinations_str
)
router_prompt = PromptTemplate(
    template=router_template,
    input_variables=["input"],
    output_parser=RouterOutputParser(),
)
router_chain = LLMRouterChain.from_llm(llm, router_prompt)

chain = MultiPromptChain(
    router_chain=router_chain,
    destination_chains=destination_chains,
    default_chain=default_chain,
    verbose=True
)